//
//  SunBankDetailListVC.m
//  Xspcf
//
//  Created by sunshine on 2017/7/21.
//  Copyright © 2017年 小小算盘. All rights reserved.
//

#import "SunBankDetailListVC.h"
#import "MJRefresh.h"

@interface SunBankDetailListVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    int _page;
    UITableViewCell *_select;
    NSString *_selectId;
}
@end

@implementation SunBankDetailListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    self.title = @"选择开户行联行号";
    [self creatUI];
    _page = 1;
    // Do any additional setup after loading the view.
}

- (void)creatUI{
    _tb = [[UITableView alloc]init];
    [self.view addSubview:_tb];
    _tb.delegate = self;
    _tb.dataSource = self;
    [_tb lt_All:SunEdgesMake(0, 0, Y(120), 0) save:YES];
    _tb.backgroundColor = BACKGROUND;
    _tb.showsVerticalScrollIndicator = NO;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    UIButton *btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    [btn lt_Top:SunEdgesMake(Y(30), X(15), X(15), Y(44)) to:_tb save:YES];
    btn.clipsToBounds = YES;
    [btn setBackgroundColor:BLUE forState:UIControlStateNormal];
    [btn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    btn.layer.cornerRadius = 4;
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:WHITE forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn addTarget:self action:@selector(setCode) forControlEvents:UIControlEventTouchUpInside];
}

- (void)requestToData{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_BANKCODELIST];
    [HttpRequest postURL:url withParam:@{@"name":[self.name stringFromUTF8],@"page":[NSString stringWithFormat:@"%d",_page]} success:^(id requestDic, NSString *msg) {
        [self.data addObjectsFromArray:requestDic[@"list"]];
        [_tb reloadData];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
        [_tb.mj_header endRefreshing];
        [_tb.mj_footer endRefreshing];
    }];
}

- (void)setCode{
    if (!_selectId) {
        [[Sun shareSun]showMsg:@"请选择开户行"];
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_SETBANKCODE];
    [[Sun shareSun]showActivity:self];
    [HttpRequest postURL:url withParam:@{@"id":_selectId} success:^(id requestDic, NSString *msg) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:@"设置成功"];
        [self.navigationController popToViewController:self.navigationController.childViewControllers[self.navigationController.childViewControllers.count - 3] animated:YES];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
        [[Sun shareSun]hideActivity];
    }];
}

- (void)refresh{
    _page = 1;
    [self.data removeAllObjects];
    [self requestToData];
}

- (void)loadMore{
    _page ++;
    [self requestToData];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic1 = self.data[indexPath.section];
    NSString *str = [NSString stringWithFormat:@"%@   (联行号:%@)",[dic1 objectForKey:@"bankDetail"],[dic1 objectForKey:@"bankCode"]];
    NSDictionary *attrs = @{NSFontAttributeName : FONT(14)};
    CGSize maxSize = CGSizeMake(KScreenW - X(80), MAXFLOAT);
    
    // 计算文字占据的高度
    CGSize size = [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
    return size.height + 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = BACKGROUND;
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (int i=0; i<cell.subviews.count; i++) {
        UIView *view = cell.subviews[i];
        [view removeFromSuperview];
        i--;
    }
    NSDictionary *dic = self.data[indexPath.section];
    UILabel *label = [[UILabel alloc]init];
    [cell addSubview:label];
    [label lt_YcenterTo:nil save:YES];
    [label lt_setLeft:X(20) to:nil Right:X(60) to:nil save:YES];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = KScreenW - X(80);
    label.font = FONT(14);
    label.text = [NSString stringWithFormat:@"%@   (联行号:%@)",[dic objectForKey:@"bankDetail"],[dic objectForKey:@"bankCode"]];
    label.adjustsFontSizeToFitWidth = YES;
    label.tag = 1;
    UIImageView *img = [[UIImageView alloc]init];
    img.tag = 2;
    [cell addSubview:img];
    [img lt_YcenterTo:nil save:YES];
    [img lt_setRight:X(18) to:nil save:YES];
    [img lt_setSize:CGSizeMake(X(20), X(20)) save:YES];
    label.textColor = BLACK;
    img.image = [UIImage imageNamed:@"address_default"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return Y(25);
    }
    return Y(10);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    _selectId = [self.data[indexPath.section] objectForKey:@"id"];
    if (_select) {
        UILabel *label = [_select viewWithTag:1];
        label.textColor = BLACK;
        UIImageView *img = [_select viewWithTag:2];
        img.image = [UIImage imageNamed:@"address_default"];
    }
    _select = cell;
    UILabel *label = [_select viewWithTag:1];
    label.textColor = RED;
    UIImageView *img = [_select viewWithTag:2];
    img.image = [UIImage imageNamed:@"address_select"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
